package com.ruitu.router_module.bean.map_location;

import android.Manifest;
import android.annotation.TargetApi;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.UiSettings;
import com.baidu.mapapi.model.LatLng;
import com.baidu.navisdk.adapter.BNCommonSettingParam;
import com.baidu.navisdk.adapter.BNaviSettingManager;
import com.beanu.arad.base.BaseModel;
import com.beanu.arad.base.BasePresenter;
import com.beanu.arad.base.my_base.MyBaseActivity;
import com.beanu.arad.utils.ToastUtils;

import java.util.ArrayList;

/**
 * Created by Administrator on 2018/4/17.
 * 地图activity基类
 */
public abstract class BaseMapActivity<T extends BasePresenter, E extends BaseModel> extends MyBaseActivity<T, E> {
    protected String permissionInfo;
    protected final int SDK_PERMISSION_REQUEST = 127;

    protected MapView mMapView;//地图view
    protected BaiduMap mBaiduMap;//百度地图对象
    protected MyLocationListener mListener;//定位监听

    protected boolean isFirsLoc = true;//是否是第一次定位(只有第一次定位成功的时候地图跳转到定位的位置)
    protected boolean isLocSuccess = false;//是否定位成功

    private MyLocationConfiguration.LocationMode mCurrentMode = MyLocationConfiguration.LocationMode.NORMAL;//定位模式:普通
    protected boolean isPermissionOk = false;//需要的权限

    //    protected List<Park> parkList = new ArrayList<>();//停车场列表
//    protected Park currPark;//当前点击的停车场
    protected AlertDialog alertDialog;//点击停车场显示的停车场信息的对话框

    protected void initMap() {
        if (null != mMapView && null != mBaiduMap) {
            mBaiduMap.setMyLocationEnabled(true);// 开启定位图层
            mBaiduMap.setMyLocationConfiguration(new MyLocationConfiguration(mCurrentMode, true, null));
            mMapView.showZoomControls(false);//不显示缩放按钮

            mBaiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {
                @Override
                public boolean onMarkerClick(Marker marker) {
                    onParkClick(marker);
                    return true;
                }
            });

            //下面是设置语音导航信息
            Bundle bundle = new Bundle();
            bundle.putString(BNCommonSettingParam.TTS_APP_ID, "11108146");// 必须设置APPID，否则会静音
            BNaviSettingManager.setNaviSdkParam(bundle);
        }
    }

    protected void setMapCenter(double lat, double lng, float zoomLevel) {//纬度,经度,缩放等级
        LatLng currLoc = new LatLng(lat, lng);
        setMapCenter(currLoc, zoomLevel);
    }

    protected void setMapCenter(LatLng currLoc, float zoomLevel) {
        MapStatus mMapStatus = new MapStatus.Builder().target(currLoc).zoom(zoomLevel).build();
        //定义MapStatusUpdate对象，以便描述地图状态将要发生的变化
        MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus);
        //改变地图状态
//        mBaiduMap.setMapStatus(mMapStatusUpdate);//直接生硬的移动地图(过去)
        mBaiduMap.animateMapStatus(mMapStatusUpdate);//动画移动地图
    }

//    protected void setParkList(List<Park> parkList) {//创建覆盖物(停车场)
//        BitmapDescriptor locIcon = BitmapDescriptorFactory.fromResource(R.mipmap.map_park_position);
//        OverlayOptions options;
//        Marker marker;
//
//        mBaiduMap.clear();//先清空所有的标识物
//        if (null != parkList && parkList.size() > 0) {
//            for (Park park : parkList) {//park必须实现序列化接口
//                double jingdu = park.getParkLongitude();
//                double weidu = park.getParkLatitude();
//
//                options = new MarkerOptions().position(new LatLng(weidu, jingdu)).icon(locIcon);
//                marker = (Marker) mBaiduMap.addOverlay(options);
//
//                Bundle bundle = new Bundle();
//                bundle.putSerializable("park", park);
//                marker.setExtraInfo(bundle);//将停车场信息设置到地图上的Marker
//            }
//        }
////        addCircleScopeToMap(currCenterLati, currCenterLongi);//根据经纬度在地图上画一个带圆心点的圆形范围
//    }

    protected void setMapEnabled(boolean enabled) {
        if (null != mBaiduMap) {
            UiSettings settings = mBaiduMap.getUiSettings();
            settings.setAllGesturesEnabled(enabled);   //关闭一切手势操作
            settings.setOverlookingGesturesEnabled(enabled);//屏蔽双指下拉时变成3D地图
            settings.setRotateGesturesEnabled(enabled);//屏蔽旋转
            settings.setZoomGesturesEnabled(enabled);//获取是否允许缩放手势返回:是否允许缩放手势
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (null != mMapView) {
            mMapView.onResume();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (null != mMapView) {
            mMapView.onPause();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != mMapView) {
            mMapView.onDestroy();
        }
    }

    @TargetApi(23)
    protected void getPersimmions() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            ArrayList<String> permissions = new ArrayList<String>();

            //定位权限为必须权限，用户如果禁止，则每次进入都会申请
            if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                permissions.add(Manifest.permission.ACCESS_FINE_LOCATION);
            }
            if (checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                permissions.add(Manifest.permission.ACCESS_COARSE_LOCATION);
            }

            if (checkSelfPermission(Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                permissions.add(Manifest.permission.READ_PHONE_STATE);
            }
            if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
            }

            if (checkSelfPermission(Manifest.permission.SYSTEM_ALERT_WINDOW) != PackageManager.PERMISSION_GRANTED) {
                permissions.add(android.Manifest.permission.SYSTEM_ALERT_WINDOW);
            }

//            // 读写权限和电话状态权限非必要权限(建议授予)只会申请一次，用户同意或者禁止，只会弹一次
//            if (addPermission(permissions, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
//                permissionInfo += "Manifest.permission.WRITE_EXTERNAL_STORAGE Deny \n";
//            }
//            if (addPermission(permissions, Manifest.permission.READ_PHONE_STATE)) {
//                permissionInfo += "Manifest.permission.READ_PHONE_STATE Deny \n";
//            }

            if (permissions.size() > 0) {
                requestPermissions(permissions.toArray(new String[permissions.size()]), SDK_PERMISSION_REQUEST);
                isPermissionOk = false;
            } else {
                isPermissionOk = true;
            }
        } else {
            isPermissionOk = true;
        }
    }

    protected void startNav() {//开启导航页面

    }

    @TargetApi(23)
    private boolean addPermission(ArrayList<String> permissionsList, String permission) {
        if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) { // 如果应用没有获得对应权限,则添加到列表中,准备批量申请
            if (shouldShowRequestPermissionRationale(permission)) {
                return true;
            } else {
                permissionsList.add(permission);
                return false;
            }
        } else {
            return true;
        }
    }

    @TargetApi(23)
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (requestCode == SDK_PERMISSION_REQUEST) {
            for (int ret : grantResults) {
                if (ret == 0) {
                    continue;
                } else {
                    ToastUtils.showLongSafe("权限不足,请在手机设置中允许相关权限");
                    return;
                }
            }
        }
    }

    protected void onParkClick(Marker marker) {

    }
}
